package elements.axis; 
import flash.display.Sprite;
import elements.axis.YTextField;
import flash.text.TextFormat;
import string.Utils;

class YAxisLabelsBase extends Sprite {
	
	var steps:Float;
	var right:Bool;
	var style:Dynamic;
	
	public function new( values:Array<Dynamic>, steps:Float, json:Dynamic, name:String, axis_name:String ) {
		super();
		this.steps = steps;
		
		var lblStyle:YLabelStyle = new YLabelStyle( json, name );
		this.style = lblStyle.style;
		
		// Default to using "rotate" from the y_axis level
		if ( json[axis_name] && json[axis_name].rotate ) {
			this.style.rotate = json[axis_name].rotate;
		}

		// Next override with any values at the y_axis.labels level
		if (( json[axis_name] != null ) && 
			( json[axis_name].labels != null ) ) {
			Object_helper.merge_2( json[axis_name].labels, this.style );
		}
		
		// are the Y Labels visible?
		if( !this.style.show_labels )
			return;
		
		// labels
		var pos:Int = 0;
		
		for ( v in values )
		{
			var tmp:YTextField = this.make_label( v );
			tmp.y_val = v.pos;
			this.addChild(tmp);
			
			pos++;
		}
	}

	//
	// use Y Min, Y Max and Y Steps to create an array of
	// Y labels:
	//
	function make_labels( min:Float, max:Float, right:Bool, steps:Float, lblText:String ):Array<Dynamic> {
		var values:Array<Dynamic> = [];
		
		var min_:Int = Math.min( min, max );
		var max_:Int = Math.max( min, max );
		
		// hack: hack: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_13989&sliceId=1
		max_ += 0.000004;
		
		var eek:Int = 0;
		var i:Int = min_;
		while ( i <= max_) {
			values.push( { val:lblText, pos:i } );
			
			// make sure we don't generate too many labels:
			if( eek++ > 250 ) break;
			i+=steps ;
		}
		return values;
	}
	
	function make_label( json:Dynamic ):YTextField
	{
		
		
		// does _root already have this textFiled defined?
		// this happens when we do an AJAX reload()
		// these have to be deleted by hand or else flash goes wonky.
		// In an ideal world we would put this code in the object
		// distructor method, but I don't think actionscript has these :-(
		
		var lblStyle:Dynamic = { };
		Object_helper.merge_2( this.style, lblStyle );
		Object_helper.merge_2( json, lblStyle );
		lblStyle.colour = string.Utils.get_colour(lblStyle.colour);
		
		var tf:YTextField = new YTextField();
		//tf.border = true;
		tf.text = this.replace_magic_values(lblStyle.val, lblStyle.pos);
		var format:TextFormat = new TextFormat();
		format.color = lblStyle.colour;
		format.font = if(lblStyle.rotate == "vertical") "spArial" else "Verdana";
		format.size = lblStyle.size;
		format.align = "right";
		tf.setTextFormat(format);
		tf.autoSize = "right";
		if (lblStyle.rotate == "vertical")
		{
			tf.rotation = 270;
			tf.embedFonts = true;
			tf.antiAliasType = flash.text.AntiAliasType.ADVANCED;
		} 
		return tf;
	}

	// move y axis labels to the correct x pos
	public function resize( left:Float, sc:ScreenCoords ):Void
	{
	}


	public function get_width():Float{
		var max:Int = 0;
		for( i in 0...this.numChildren)
		{
			var tf:YTextField = cast( this.getChildAt(i), YTextField);
			max = Math.max( max, tf.width );
		}
		return max;
	}
	
	public function die(): Void {
		
		while ( this.numChildren > 0 )
			this.removeChildAt(0);
	}

	function replace_magic_values(labelText:String, yVal:Float):String {
		labelText =  StringTools.replace(labelText,'#val#', NumberUtils.formatNumber(yVal));
		return labelText;
	}
}
